const Mock = require(`mockjs`)
const express = require(`express`)
const app = express()


function generatorList(num) {
    return Mock.mock({
        [`list|${num}`]: [{
            //模拟ID,自增方式追加
            'id|+1': 1,
            title: '@ctitle(15,25)',
            image: "@natural(0,15)",
            reads: "@natural(0,9999)",
            from: "@ctitle(3,10)",
            date: "@date('yyyy-MM-dd')"
        }]
    }
    )
}

//允许跨域

app.all('/cors-server', (request, response) => {
    // 设置响应头
    // 设置允许跨域 
    response.setHeader('Access-Control-Allow-Origin', '*')
    // 设置允许使用自定义的请求头字段
    response.setHeader('Access-Control-Allow-Headers', '*')
    // 设置允许任意的HTTP请求方法访问
    response.setHeader('Access-Control-Allow-Method', '*')
    next()
})

app.get('/data', function (req, res) {
    const { num } = req.query
    return res.send(generatorList(num))
})


const server = app.listen(4000, function () {
    console.log("本地mock服务启动,接口地址为:http://localhost:4000/data?num=请求列表数量")
})